package com.geese.module.admin.controller;

import java.util.List;
import java.util.Map;

import com.geese.common.entity.Business;
import com.geese.exception.BusinessException;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.geese.common.BaseController;
import com.geese.common.constants.Constants;
import com.geese.common.entity.School;
import com.geese.common.util.BeanUtil;
import com.geese.common.util.TextValidator;
import com.geese.common.vo.Page;
import com.geese.common.vo.Paginator;
import com.geese.module.admin.vo.SchoolQuery;
import com.geese.module.admin.vo.SchoolVO;
import com.geese.module.ceo.service.SchoolService;
import com.geese.module.user.service.UserService;
import com.google.common.base.Function;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * @author YeFangYi 
 */
@Controller
@RequestMapping(value = "/admin")
@Scope("prototype")
public class SchoolController extends BaseController {

    @Autowired
    private UserService userService;
    @Autowired
    private SchoolService schoolService;


    /**
     * 新增学校
     */
    @PostMapping("/school/save")
    @ResponseBody
    public String schoolSave(@RequestBody SchoolVO schoolVO, @RequestParam String token){
    	if(StringUtils.isEmpty(schoolVO.getCode())) {
    		throw new BusinessException(120017);
    	}
    	if(StringUtils.isEmpty(schoolVO.getName())) {
    		throw new BusinessException(120018);
    	}
    	if(!StringUtils.isNumeric(schoolVO.getCardinalNumber())) {
    		throw new BusinessException(120019);
    	}
    	if(StringUtils.isNotEmpty(schoolVO.getManagerMobile()) && !TextValidator.isMobileExact(schoolVO.getManagerMobile())) {
    		throw new BusinessException(120020);
    	}
    	
    	userService.checkUser(token);
    	School school = BeanUtil.map(schoolVO, School.class);
    	schoolService.save(school);
        return response();
    }

    /**
     * 批量删除学校
     */
    @PostMapping("/school/batchRemove")
    @ResponseBody
    public String schoolBatchRemove(@RequestParam String token, @RequestParam String ids){
    	List<String> tempIds = Splitter.on(",").trimResults().splitToList(ids);
    	if(CollectionUtils.isEmpty(tempIds)) {
    		throw new BusinessException(120016);
    	}
    	List<Integer> schoolIds = Lists.transform(checkIds(tempIds), new Function<String, Integer>() {
			@Override
			public Integer apply(String tempId) {
				return Integer.valueOf(tempId);
			}
		});
    	userService.checkUser(token);
    	schoolService.batchRemove(schoolIds);
        return response();
    }

    /**
     * 修改学校
     */
    @PostMapping("/school/update")
    @ResponseBody
    public String schoolUpdate(@RequestBody SchoolVO schoolVO, @RequestParam String token){
    	if(!StringUtils.isNumeric(schoolVO.getId())) {
    		throw new BusinessException(120015);
    	}
    	if(StringUtils.isEmpty(schoolVO.getCode())) {
    		throw new BusinessException(120017);
    	}
    	if(StringUtils.isEmpty(schoolVO.getName())) {
    		throw new BusinessException(120018);
    	}
    	if(!StringUtils.isNumeric(schoolVO.getCardinalNumber())) {
    		throw new BusinessException(120019);
    	}
    	if(StringUtils.isNotEmpty(schoolVO.getManagerMobile()) && !TextValidator.isMobileExact(schoolVO.getManagerMobile())) {
    		throw new BusinessException(120020);
    	}
    	
    	userService.checkUser(token);
    	School school = BeanUtil.map(schoolVO, School.class);
    	schoolService.update(school);
        return response();
    }
    
    /**
     * 学校列表
     */
    @GetMapping("/school/listInPage")
    @ResponseBody
    public String schoolList(SchoolQuery query) {
    	userService.checkUser(query.getToken());
    	
    	Page page = getPageInfo(query);
    	Paginator<School> pageList = schoolService.listAllByQuery(query, page);
    	Map<String, Object> result = Maps.newHashMap();
    	result.put(Constants.PAGE_INFO, pageList.getPageResult());
    	result.put("schoolList", pageList.getResults());
        return response(result);
    }
}
